import 'package:flutter/material.dart';
import 'package:yp/services/api.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';

import 'state.dart';

class AddressListLogic extends GetxController {
  final state = AddressListState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    state.isPersonalCenter =
    Get.parameters != null && Get.parameters['isPersonalCenter'] != null ?
    (Get.parameters['isPersonalCenter'] == 'true' ? true : false) : false;
  }

  @override
  void onReady() {
    super.onReady();
    print(' Get.arguments');
    print(state.goodsList.length);

    loadData();
  }

  loadData() {
    var userNumId = Get.arguments != null && Get.arguments['userNum'] != null
        ? Get.arguments['userNum']
        : SpUtil.getInt('empeNumId');
    var params = {
      'usr_num_id': userNumId,
      'access_token': SpUtil.getString('token'),
    };
    print("AddressListLogic---${Get.arguments} ---- ${SpUtil.getString(
        'userNumId')} --- $userNumId");
    //获取选择商品列表数据
    Api.addressList(params).listen((event) {
      if (event['code'] == 0) {
        state.goodsList = event["member_adrs"];
      } else {
        state.goodsList = [];
      }
      update();
    }, onError: (error) {
      print('地址回去失败 ===== error = $error');
    });
  }

  dedAss(id) {
    var params = {
      "adr_num_id": id, //地址编号
      'access_token': SpUtil.getString('token'),
    };

    Api.deleteAss(params).listen((event) {
     onReady();
      update();
    }, onError: (error) {

    });
  }
}
